complex models, such as multiple output models, a direction-free graph, and so on.In the next section of this article, we will study the theories and examples of the Keras sequential models and functional APIs.4. Keras Sequential Models In this section, I will introduce the theory of Keras sequential models in the future. I will quickly explain how it works and
fine-tuning (iii)4, Keras series ︱ Facial Expression Classification and recognition: OpenCV Face Detection +keras emotional Classification (iv)5, Keras series of ︱ Migration learning: Using InceptionV3 for fine-tuning and forecasting, complete case (v)
. One, CIFAR10 small picture Classification example (sequential type)
To train a model, you first have to know
Keras provides many common, prepared layer objects, such as the common convolution layer, the pool layer, and so on, which we can call directly through the following code:
# Call a conv2d layer
from Keras import layers
conv2d = Keras.layers.convolutional.Conv2D (filters,\ kernel_size
, \
strides= (1, 1), \
padding= ' valid ', \
...)
However, in practical applications, we often need to build some layer obje
= Train (d[0), d[1]) p Rint (Final model:) Print (W.get_value ()) print (B.get_value ()) print ("target values for D:") print ("d[1]" Prediction on D: ") print (Predict (d[0))
We found that building a model using Theano typically requires the following steps:
0) Preprocessing data
# Generate a dataset:d = (input_values, target_class)
1) Define Variables
# Declare Theano Symbolic variables
2) Building (diagram) model
# construct Theano Expression graph
3) compiling model, th
version. Second, Keras use the tutorial below a simple example, more examples can oneself to the official website of the document tutorial to see, the official website gave a very detailed tutorial, unlike Caffe documents so few. Take a look at the following example, loosely constructing the CNN model. Keras provides us with two network models. 1, one is the CNN comparison commonly used sequential network
article. These include features that require Keras, as well as data loading from pandas and data preparation and model evaluation from Scikit-learn.
Import numpy
Import pandas from
keras.models import sequential from
keras.layers import dense
from Keras.wrappers.scikit_learn import kerasclassifier from
keras.utils import np_utils from
sklearn.model_ Selection import Cross_val_score from
sklearn.model_sele
model = sequential ()
You can simply use. Add () to stack the model:
From keras.layers import dense
model.add (dense (units=64, activation= ' Relu ', input_dim=100))
Model.add (Dense (units=10, activation= ' Softmax '))
Once you have finished building the model, you can configure the learning process with. Compile ():
Model.compile (loss= ' categorical_cross
calculations. As for how to deal with input layer after transforming from 28*28 to 784, we don't need to care about it. (like the study of students can go to the source code).
Also, the Keras input is in the form of (Nb_samples, Input_dim): That is, the number of samples, the input dimension.
5) Sample Code
From keras.models import sequential to Keras.layers.core import dense, dropout, activation from Kera
Python Keras module 'keras. backend' has no attribute 'image _ data_format ', keraskeras. backendProblem:
When the sample program mnist_cnn is run using Keras, the following error occurs: 'keras. backend' has no attribute 'image _ data_format'
Program path https://github.com/fchollet/
Keras is a Theano and TensorFlow-compatible neural network Premium package that uses him to component a neural network more quickly, and several statements are done. and a wide range of compatibility allows Keras to run unhindered on Windows and MacOS or Linux.Today to compare learning to use Keras to build the following common neural network:
Regression
Softmax is also placed in the activations module (I think it is more reasonable to put in the layers module). In addition, the newer activation functions, such as Leakyrelu and Prelu, are provided Keras in the Keras.layers.advanced_activations module.
InitializationsThis is the parameter initialization module, which initializes the call to Init when the layer is added. Keras provides uniform, lecun_unifo
'], merge_mode= ' sum ') graph.compile (' Rmsprop ', {' Output ': ' MSE '}) History = Graph.fit ({' input1 ': X_train, ' input2 ': x2_train, ' ouTput ': Y_train}, nb_epoch=10) predictions = graph.predict ({' input1 ': x_test, ' Input2 ': X2_test}) # {' Output ': ...}
How to use a rule item
A rule item is a penalty term for a weight parameter. It is included in the cost function.In Keras dense Layer, Timed
effect prediction class
Put the trainer class code first, and define the network here. The most important one is just as important as the data format (haha, the data format is very important, in this program)
Import SQL Injection Dataimport numpy as npimport kerasfrom keras. models import Sequentialfrom keras. layers import Dense, Dropout, Activationfrom
,... | class_8| 2.0| [ -0.2535060296260...| | [0.0,0.0,0.0,0.0,... | class_7| 5.0| [ -0.2535060296260...| +--------------------+--------+--------------+--------------------+ only showing top rows
Keras Deep Learning model
Now so we have a data frame with processed features and labels, let ' s define a deep neural net so we can use to addres s the classification problem. Chances are you came this because you know a thing or two
...| | [0.0,0.0,0.0,0.0,... | class_3| 3.0| [ -0.2535060296260...| | [0.0,0.0,4.0,0.0,... | class_8| 2.0| [ -0.2535060296260...| | [0.0,0.0,0.0,0.0,... | class_7| 5.0| [ -0.2535060296260...| +--------------------+--------+--------------+--------------------+ only showing top rows
Keras Deep Learning model
Now, we had a data frame with processed features and labels, let's define a deep neural net the We can use to addr
, momentum=0.9, decay=0.0, Nesterov=false)
model.fit (train_set_x, train_set_y, validation_split=0.1, nb_epoch=200, batch_size=256, Callbacks=[lrate])
The above code is to make the learning Rate index drop, as shown in the following figure:
Of course, can also directly modify the parameters in the SGD declaration function to directly modify the learning rate, learning rate changes as follows:
SGD = SGD (lr=learning_rate, Decay=learning_rate/nb_epoch, momentum=0.9, Nesterov=true)
You can refer
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.